package com.ms.education.statistics.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.ms.education.statistics.entity.StatisticsDaily;
import com.ms.education.statistics.mapper.StatisticsDailyMapper;
import com.ms.education.statistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ms.education.statistics.service.feign.CourseFeignService;
import com.ms.education.statistics.service.feign.UcenterFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-01-22
 */
@Service
public class StatisticsDailyServiceImp extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private UcenterFeignService ucenterFeignService;

    @Autowired
    private CourseFeignService courseFeignService;

    @Override
    public void createDailyDate(String date) {

        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(date);

        //注册量
        statisticsDaily.setRegisterNum((Integer) ucenterFeignService.countRegisterDay(date).getData().get("count"));

        //统计每日登陆人数
        statisticsDaily.setLoginNum(RandomUtil.randomInt(100, 300));

        //统计视频播放数
        statisticsDaily.setVideoViewNum(Math.toIntExact(redisTemplate.opsForHyperLogLog().size(date + ":video-play")));

        //统计每日新增课程数
        statisticsDaily.setCourseNum((Integer) courseFeignService.countCourseAdd(date).getData().get("count"));

        this.save(statisticsDaily);
    }


}
